<?php

require_once './include/common.inc.php';
require_once './include/country.php';
require_once 'left.php';
require_once 'right.php';

$page_title = 'Acuity World WebShop';
$menu_id = 0;
$menu_str = PrintMenu();
$alert_message = '';
$jsFile = '<script src="css/memberSignUp.js" type="text/javascript" language="javascript"></script>';
$countryStr1 = FFPCountry("Denmark","country");
$countryStr2 = FFPCountry("Denmark","country2");

$act = $_POST['act'];

if ($act == "" && isLogin()) {
    header("Location: shopping_cart.php?checkout");
    exit;
}

if ($act == "register") {
    $username = $_POST['username'];
    $password = $_POST['password']; 
    $verify = $_POST['verify']; 
    
    $email = $_POST['email']; 
    $company = filter($_POST['company']); 
    $firstname = htmlspecialchars($_POST['firstname']); 
    $lastname = htmlspecialchars($_POST['lastname']); 
    
    $street =filter( $_POST['street']); 
    $city = filter($_POST['city']); 
    $state = filter($_POST['state']); 
    $zip = filter($_POST['zip']); 
    $country = filter($_POST['country']); 
    
    $street2 = filter($_POST['street2']); 
    $city2 =filter( $_POST['city2']); 
    $state2 = filter($_POST['state2']); 
    $zip2 = filter($_POST['zip2']); 
    $country2 = filter($_POST['country2']); 
    
    $phone1 = $_POST['phone1'];
    $phone = filter($phone1 . "-" . $_POST['phone']);
    $mobile1 = $_POST['mobile1'];
    $mobile = filter($mobile1 . "-" . $_POST['mobile']);
    $fax1 = $_POST['fax1'];
    $fax = filter($fax1 . "-" . $_POST['fax']);
    
    $faq1 = isset($_POST['faq1']) && $_POST['faq1'] == 1 ? 1 : 0;
    $faq2 = isset($_POST['faq2']) && $_POST['faq2'] == 1 ? 1 : 0;
    
    //echo "<pre>";print_r($_POST);exit;
    
    if (trim($email) == "" ||trim($password) == "" ||trim($company) == "" ||  trim($firstname) == "" || trim($lastname) == "" ||  trim($street) == "" || trim($city) == "" || trim($zip) == "" || trim($country) == "" || trim($phone1) == "" || trim($phone) == "" ) {
        echo "<script>alert('Fields marked with an asterisk * are required');history.back();</script>";
        exit;
    }
    
    if ($password != $verify) {
        echo "<script>alert('Invalid password or verify');history.back();</script>";
        exit;
    }
    
    if (!preg_match("/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/", $email)) {
        echo "<script>alert('Invalid email, Please try again');history.back();</script>";
        exit;
    }
    
    // ?email?h
    $sql = "SELECT `id` FROM `cus_members` WHERE `email` = '$email'";
    $rs = $db->get_one($sql);
    if (!empty($rs)) {
        echo "<script>alert('Email exist.');history.back(1);</script>";
        exit();
    }
    
    
    $sql = "INSERT INTO `cus_members` (`email` ,`password1` , `password2` ,  `company` , `firstname` , `lastname` , `street` , `city` , `state` , `zip` , `country` , `street2` , `city2` , `state2` , `zip2` , `country2` , `phone` , `mobile` , `fax` , `faq1` , `faq2` , `date` ) 
            VALUES ( '$email', '" . md5($password) . "', '$password', '$company', '$firstname', '$lastname', '$street', '$city', '$state', '$zip', '$country', '$street2', '$city2', '$state2', '$zip2', '$country2', '$phone', '$mobile', '$fax', '$faq1', '$faq2', '$timestamp'
            )";
    $db->query($sql);
    $user_id = $db->insert_id();
    
    // ' send_email
    $show_name = empty($lastname) ? $firstname : $firstname . ' ' . $lastname;
    $reg_time = date("Y-m-d", $timestamp);
    $faq2_show = $faq2==1 ? "Yes" : "No";

    require_once './mail/regist_to_members.php';
    sendhtmlmail($from_name,$from_email,$to_name,$to_email,$subject,$content);
    
    require_once './mail/regist_to_webmaster.php';
    sendhtmlmail($from_name,$from_email,$to_name,$to_email,$subject,$content);
    
	//clear all cookies  and session first
	if(isPromoterLogin())
	{
		Cookie('webshoppromoter','',0);
	}   
		
    Cookie("webshopuser",base64_encode($user_id."\t".$username."\t".$timestamp),null);
	Cookie("myCart_signdate",$timestamp,null);
	
    header("Location: shopping_cart.php?checkout");
    exit;
    
}



require_once PrintEot('header');
require_once PrintEot('left');
require_once PrintEot('member_regist');
require_once PrintEot('right');
require_once PrintEot('footer');

?>